﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TFSExceptionHandling.Domain.Model.TFS;
using TFSExceptionHandling.Domain.TFSHandling;
using TFSExceptionHandling.Infrastructure.TFSHandling;

namespace TFSExceptionHandling.DistributedServices.TFS
{
    public class TfsHandling : ITfsHandling
    {
        #region " ITfsHandling Members "

        ///<see cref="ITfsHandling"/>
        public WorkItemHandling RegisterWorkItem(WorkItemHandling workItemHandling, TfsHandlingConnection tfsHandlingConnection)
        {
            var tfs = TfsHandlingHelper.ConnectTfs(tfsHandlingConnection);

            //Get data store that contains all workitems on a particular server
            var store = tfs.GetService<WorkItemStore>();

            //Get particular Team Project
            Project project = store.Projects[tfsHandlingConnection.ProjectName];

            //Create new workitem of type Bug in the project
            WorkItem workItem = project.WorkItemTypes[workItemHandling.WorkItemType].NewWorkItem();

            //TEAM FOUNDATION SERVICE
            workItem.Fields["Title"].Value = workItemHandling.Title;
            workItem.Fields["Assigned To"].Value = workItemHandling.AssignedTo;
            workItem.Fields["Repro Steps"].Value = workItemHandling.ReproSteps;
            workItem.Fields["Description"].Value = workItemHandling.Description;
            workItem.Fields["Team Project"].Value = workItemHandling.TeamProject;

            //Set properties of task like title, iteration, activity, assigned to - LOCAL
            //workItem.Fields[(int)EnumWorkItemFields.Title].Value = workItemHandling.Title;
            //workItem.Fields[(int)EnumWorkItemFields.AssignedTo].Value = workItemHandling.AssignedTo;
            //workItem.Fields[(int)EnumWorkItemFields.ReproSteps].Value = workItemHandling.ReproSteps;
            //workItem.Fields[(int)EnumWorkItemFields.Description].Value = workItemHandling.Description;
            //workItem.Fields[(int)EnumWorkItemFields.TeamProject].Value = workItemHandling.TeamProject;

            var result = workItem.Validate();

            if (result.Count != 0) return null;

            //Save the task
            workItem.Save();

            workItemHandling.Id = workItem.Id;

            TfsHandlingHelper.DisconnectTfs();

            return workItemHandling;
        }

        //public WorkItem GetWorkItem(int id)
        //{
        //    var tfs = TfsHandlingHelper.ConnectTfs();

        //    //Get data store that contains all workitems on a particular server
        //    var store = tfs.GetService<WorkItemStore>();

        //    WorkItem workItem = store.GetWorkItem(id);

        //    return workItem;
        //}

        //public WorkItemCollection GetWorkItems()
        //{
        //    var tfs = TfsHandlingHelper.ConnectTfs();

        //    //Get data store that contains all workitems on a particular server
        //    var store = tfs.GetService<WorkItemStore>();

        //    // Define a query that uses a variable for the type of work item. 
        //    const string queryString = "Select [State], [Title] From WorkItems Where [Work Item Type] = @Type";

        //    // Set up a dictionary to pass "User Story" as the value of the type variable.
        //    var variables = new Dictionary<string, string> { { "Type", WorkItemTypes.Bug.ToString() } };

        //    // Create and run the query.
        //    var query = new Query(store, queryString, variables);
        //    WorkItemCollection workItems = query.RunQuery();

        //    return workItems;
        //}

        #endregion
    }
}
